<?php

/* Script: podcastxml.php
 * Description: Create an XML feed for podcast by reading a folder of MP3's and reading the ID3 tag for each file.
 * History:
 * Author				Date			Description
 * Sherif Mansour		05/11/2008		Initial Version
 * Sherif Mansour		3/10/2009		Fixed HTML Entities Bug
 */

/* START: Script Configuration */
	error_reporting(E_ALL);	//Error reporting flag
	$cfg = array();			//The Main Config Array

	/* START: Method of operation.*/
		/* This defines how you want the script to work. You have two options:
		 * 'file' OR 'stdout'
		 * If you chose 'file': This means every time this script runs, it will write the podcast to a file you specify. Use this if there are many podcasts (better perfomance, and schedule a cron job)
		 * If you choose 'stdout': This will write the podcast XML to the browser using this file itself. Us this if there arent many podcasts, and can affort to have this script run every time someone access it.
		 */
		$cfg['operation'] = "stdout";			//Values can be 'file' OR 'stdout'
		$cfg['file_name'] = "podcast.xml";	//If you have selected file, enter the name of the file
		$cfg['file_location'] = "./";		//If you have selected file, enter the relative location of where you want to write the xml file. Dont forget trailing slash!
											//NOTE: If you have selected the 'file' mode of operation, make sure you CHMOD that folder to 777 where you want to write to!
	/* END: Method of operation. */


    /* START: Config values */
        $cfg['max_feed'] = 10;                                                                          //Maximum number of files to display in the feed
        $cfg['tag_title'] = "Title of the podcast";                                                     //The Podcast Title
        $cfg['tag_description']= "Description of the podcast";                                          //The Poddcast Description
        $cfg['tag_link'] = "http://www.blog.sherifmansour.com";                                         //The URL for the person providing the podcast
        $cfg['tag_lang'] = "en-us";                                                                     //Language Tag
        $cfg['tag_copyright'] = "Any copyright statement";                                              //Copyright
        $cfg['tag_owneremail'] = "ownersemail@yourwebsite.com";                                         //Author's email
        $cfg['tag_generator']= "PHP Podcast Generator - blog.sherifmansour.com";                        //Generator name (This script!)
        $cfg['tag_ttl']= 60;                                                                            //Podcast Time to Live
        $cfg['root_url'] = "http://www.yoursite.com/podcasts/";                                         //Root MP3 of the Podcasts (Absolute HTTP Path, with Trailing Slash)
        $cfg['abs_url'] = ".";                                                                          //Absolute reference of where the MP3's are stored on the server
        $cfg['lib_includes'] = "podcast_libs.php";                                                      //Lib files used for this script
    /* END: Config values */

/* END: Script Configuration */


/*****************************************************************
 * DO NOT EDIT PAST THIS POINT UNLESS YOU KNOW WHAT YOU ARE DOING
 *****************************************************************/

/* START: Script, do not edit past this point */
	require($cfg['lib_includes']);
	$output_string = "";

	/* Compile header string */
		$cfg['root_url'] =  substr($cfg['root_url'], 0, strrpos ($cfg['root_url'], "/")); // Trim off script name itself
		$output_string .= "<?xml version=\"1.0\"?>\n";
		$output_string .= "<rss version=\"2.0\">\n";
		$output_string .="	<channel>\n";
		$output_string .="		<title>".$cfg['tag_title']."</title>\n";
		$output_string .="		<link>".$cfg['tag_link']."</link>\n";
		$output_string .="		<description>".$cfg['tag_description']."</description>\n";
		$output_string .="		<lastBuildDate>" . date("r") ."</lastBuildDate>\n";
		$output_string .="		<language>".$cfg['tag_lang']."</language>\n";
		$output_string .="		<copyright>".$cfg['tag_copyright']."</copyright>\n";
		$output_string .="		<generator>".$cfg['tag_generator']."</generator>\n";
		$output_string .="		<webMaster>".$cfg['tag_owneremail']."</webMaster>\n";
		$output_string .="		<ttl>".$cfg['root_url']."</ttl>\n\n";

	/* Loop through the folder and compile the body tags in a string for each MP3 */
		$dirArray = getDir($cfg['abs_url']);	// Get a list of the current directory

		while (list($filename, $filedate) = each($dirArray) AND $cfg['max_feed'] > 0) {
			$mp3file = new CMP3File;
			$mp3file->getid3 ($filename);
			$output_string .= "<item>\n";
			$output_string .= "<title>".htmlentities($mp3file->title)."</title>\n";
			$output_string .= "<link>".$cfg['root_url']."/". str_replace(" ", "%20", $filename) ."</link>\n";
			$output_string .= "<description>by ".htmlentities($mp3file->artist). htmlentities($mp3file->album)." \n".htmlentities($mp3file->comment)."</description>\n";
			$output_string .= "<pubDate>".date("r",$filedate)."</pubDate>\n";
			$output_string .= "<enclosure url=\"".$cfg['root_url']."/". str_replace(" ", "%20", $filename) ."\" length=\"";
			$output_string .= filesize($filename);
			$output_string .= "\" type=\"audio/mpeg\"/>\n";	// Training slash for XML
			$output_string .= "</item>\n\n";
			$cfg['max_feed']--;
		}

	/* Compile the footer string*/
		$output_string .= "</channel>\n</rss>\n";

	/* START: Writing string to desired output */
		switch ($cfg['operation']){
			case 'stdout':	header('Content-type: text/xml', true);
							echo $output_string;
							break;
			case 'file':	$output_file = $cfg['file_location'].$cfg['file_name'];
							$fh = fopen($output_file, 'w') or die("Error: Cant open file for writing. Check you have the right file, file path and directory is writable.");;
							ftruncate($fh, 0);
							fwrite($fh, $output_string);
							fclose($fh);
							break;
			default:		echo "Error: You havent selected a valid mode of operation in the cfg['operation'] array. Possible values are 'file'|'stdout'";
		}	
	/* END: Writing to desired output */
/* END: Script */
?>